<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="/css/plat-style.css">
    <style>
        thead tr th{text-align: center!important;font-weight: bold!important;}
        tr td{text-align: center!important;}
        .layui-table img {
            width: auto;
            height: auto;
            max-width: 100% !important;
            max-height: 100%;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <form action="/app/recommendAdd" class="layui-form" id="seachFrom">
                    <div class="layui-card-body">
                        <div class="layui-container" style="padding-bottom: 50px">
                            <div class="layui-col-sm12">
                                <div class="layui-col-sm4">
                                    <div class="layui-col-sm4" style="line-height: 38px">一级分类：</div>
                                    <div class="layui-col-sm7">
                                        <select name="mainClassifyFirstId" data-type="1">
                                            <option value="">请选择</option>
                                            <c:forEach items="${firstList}" var="a">
                                                <option value="${a.id}">${a.name}</option>
                                            </c:forEach>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-col-sm4">
                                    <div class="layui-col-sm4" style="line-height: 38px">二级分类：</div>
                                    <div class="layui-col-sm7">
                                        <select name="mainClassifySecondId" class="secondClassify" data-type="2">
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-col-sm4">
                                    <div class="layui-col-sm4" style="line-height: 38px">三级分类：</div>
                                    <div class="layui-col-sm7">
                                        <select name="mainClassifyThirdId" class="thirdClassify">
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-sm12" style="margin-top: 10px">
                                <div class="layui-col-sm4">
                                    <div class="layui-col-sm4" style="line-height: 38px">商品编码：</div>
                                    <div class="layui-col-sm7">
                                        <input type="text" class="layui-input" name="proCode">
                                    </div>
                                </div>
                                <div class="layui-col-sm2 layui-col-sm-offset2" style="line-height: 38px">
                                    <button class="layui-btn layui-btn-sm" type="submit">查询</button>
                                </div>
                            </div>
                            <div class="layui-col-xs12">&nbsp;</div>
                        </div>
                        <table class="layui-table">
                            <thead>
                            <tr>
                                <th>&nbsp;</th>
                                <th>商品编码</th>
                                <th>商品名称</th>
                                <%--<th>规格名称</th>--%>
                                <th>价格</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:forEach items="${pageInfo.records}" var="l">
                                <tr>
                                    <td width="5%">
                                        <input type="checkbox" lay-skin="primary" value="${l.proSkuId}" data-pro="${l.proCode}" ${l.checkFlag}>
                                    </td>
                                    <td width="10%">${l.proCode}</td>
                                    <td width="10%">${l.proName}</td>
                                    <%--<td width="10%">${l.specName}</td>--%>
                                    <td width="10%"><span class="red">${l.price}</span></td>
                                </tr>
                            </c:forEach>
                            </tbody>
                        </table>
                        <div class="test-table-reload-btn" style="margin-top: 10px;">
                            <%@include file="../paging.jsp" %>
                        </div>
                    </div>
                </form>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn saveThis">保存</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<input type="hidden" value="${parentId}" id="parentId">
<script src="/layuiadmin/layui/layui.js"></script>
<script type="text/javascript" src="/js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function(){
        var $ = layui.$,
            form = layui.form,
            element = layui.element,
            layer = layui.layer;
        element.render();

        form.on('select',function (data) {
            //得到select原始DOM对象
            var dom = data.elem;
            //得到选择下拉框类型，1一级分类 2二级分类
            var selectType = $(dom).data("type");
            //得到点击下拉框类型，10一级分类 20二级分类
            //此处是重新选择时需要清空其它下拉框的数据
            var clickType = $(dom).data('click');
            //得到被选中元素的id
            var id = data.value;
            //selectType为1时代表是选择行业
            if(selectType === 1){
                $.ajax({
                    url: "/classify/queryClassifyListByAjax",
                    data: {"pid":id,"type":3,"flag":2},
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        var html = '<option value="">请选择</option>';
                        $.each(data, function (i, v) {
                            html += '<option value="' + v.id + '" >' + v.name + '</option>'
                        });
                        $(".secondClassify").append(html);
                        //重新渲染下拉框元素
                        form.render('select');
                    }
                });
            }else if(selectType === 2){
                $.ajax({
                    url: "/classify/queryClassifyListByAjax",
                    data: {"pid":id,"type":3,"flag":3},
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        var html = '<option value="">请选择</option>';
                        $.each(data, function (i, v) {
                            html += '<option value="' + v.id + '" >' + v.name + '</option>'
                        });
                        $(".thirdClassify").append(html);
                        //重新渲染下拉框元素
                        form.render('select');
                    }
                });
            }
        });

        var skuDetailId;
        var proCode;
        form.on('checkbox',function (data) {
            var dom = data.elem;
            var pro = $(dom).data('pro');
            form.render('checkbox');
            if(data.elem.checked){
                skuDetailId = data.elem.value;
                proCode = pro;
            }else{}
        });

        $('.saveThis').on('click',function () {
            // layer.msg(skuDetailId + " / " + proCode);
            var parentId = $("#parentId").val();
            $.ajax({
                url: "/app/addRecommend",
                data: {"skuDetailId":skuDetailId,"proCode":proCode,"parentId":parentId},
                dataType: "json",
                type: "post",
                traditional: true,
                success: function (data) {
                    if (data.status) {
                        layer.msg(data.desc);
                        window.setTimeout(function () {
                            window.parent.location.reload();
                        }, 1000);
                    }else{
                        layer.msg(data.desc,{icon:2});
                    }
                }
            });
        });
    });
</script>
</body>
</html>